import { IEchartsDataItem } from '@/model/echarts/echartsInter'
import { CallbackDataParams, LabelFormatterCallback, MarkPointOption } from 'echarts/types/dist/shared'
import { MarkPointDataItemOption } from 'echarts/types/src/component/marker/MarkPointModel'

function contains (arr:Array<string>, obj:string) {
  let flag = false
  if (arr.length > 0) {
    arr.forEach(a => {
      if (a === obj) {
        flag = true
      }
    })
  }
  return flag
}

/* 判断两层数组对象是否有数据 */
export function dataFlag (datas:Array<IEchartsDataItem>) {
  let flag = 0
  let value = []
  if (datas.length === undefined || datas.length === null || datas.length === 0) { // 没有数据
    flag = flag + 1
  } else {
    datas.forEach((data:IEchartsDataItem, i:number) => {
      value = data.value
      if (value === undefined || value == null || value.length == null || value.length === 0) {
        flag = flag + 1
      }
    })
  }
  return flag
}
/* 无数据情况 */
export function noSeries () {
  return {
    type: 'pie',
    markLine: {
      show: false
    },
    data: [{
      value: 0,
      itemStyle: {
        color: 'rgba(0,0,0,0)'
      },
      label: {
        lineHeight: '26',
        formatter: '暂无数据',
        position: 'center', // 让文字居中
        show: true,
        fontSize: 14,
        color: '#fff'
      }
    }]
  }
}

/* 线型图、柱状图类目轴数据处理 */
export function categoryData (category:Array<string>, name:string) {
  if (!contains(category, name)) {
    category.push(name)
  }
  return category
}

/* 线型图、柱状图等不堆叠时求最大值 方便对grid等数值进行处理 */
export function valueMax (max:number, value:number) {
  if (parseFloat(String(value)) > parseFloat(String(max))) {
    max = value
  }
  return max
}

/* 线型图、柱状图等y轴是类目轴时求文本长度 方便对grid等数值进行处理 */
export function nameLength (max:number, name:string) {
  if (name.length > max) {
    max = name.length
  }
  return max
}

export function markPoint (datas:IEchartsDataItem):MarkPointOption {
  const data:Array<MarkPointDataItemOption> = [{
    name: datas.name,
    type: 'max',
    symbol: 'circle',
    symbolSize: 6,
    label: {
      show: false
    },
    itemStyle: {
      color: '#FFA200'
    }
  }]
  data.unshift({
    name: datas.name,
    type: 'max'
  })
  return {
    symbol: 'pin',
    symbolSize: 32,
    itemStyle: {
      color: '#1A6481'
    },
    label: {
      color: '#fff',
      offset: [0, -5],
      padding: [8, 5, 5, 5],
      formatter: function (param:CallbackDataParams) {
        return param.value + datas.unit
      } as LabelFormatterCallback,
      backgroundColor: '#1A6481',
      borderRadius: 3
    },
    data: data
  } as MarkPointOption
}

/* 柱状图是堆叠类型时求每个类目堆叠后得和数组 方便获取最大值对grid等数值进行处理 */
export function stackData (data: Array<IEchartsDataItem>): Array<{value: Array<{name:string, value:number}>}> {
  const stackData: Array<{value:Array<{name:string, value:number}>}> = []
  if (data) {
    data.forEach(item => {
      if (!stackData.length) {
        stackData.push({ value: item.value })
      } else {
        stackData.forEach((s, i) => {
          s.value.forEach((v, j) => {
            item.value.forEach(t => {
              if (v.name === t.name) {
                let val = 0
                val = v.value
                stackData[i].value[j].value = val + t.value
              }
            })
          })
        })
      }
    })
  }
  return stackData
}

interface IEchartsItem {
  name:string
  value:number
}
/* 数组对象排序 */
export function sortObj (value:Array<IEchartsItem>, flag?:boolean) {
  const data = JSON.parse(JSON.stringify(value))
  if (flag) {
    data.sort((a:IEchartsItem, b:IEchartsItem) => {
      /* 升序 */
      return a.value - b.value
    })
  } else {
    /* 降序 */
    data.sort((a:IEchartsItem, b:IEchartsItem) => {
      return b.value - a.value
    })
  }
  return data
}
